package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.PartyProbationary;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PartyDevelopmentObject;
import com.ruoyi.system.service.IPartyDevelopmentObjectService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发展对象申请Controller
 * 
 * @author ruoyi
 * @date 2025-10-16
 */
@RestController
@RequestMapping("/system/object")
public class PartyDevelopmentObjectController extends BaseController
{
    @Autowired
    private IPartyDevelopmentObjectService partyDevelopmentObjectService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询发展对象申请列表
     */
    //("@ss.hasPermi('system:object:list')")
    @GetMapping("/list")
    public TableDataInfo list(PartyDevelopmentObject partyDevelopmentObject)
    {
        startPage();
        List<PartyDevelopmentObject> list = partyDevelopmentObjectService.selectPartyDevelopmentObjectList(partyDevelopmentObject);
        return getDataTable(list);
    }

    /**
     * 导出发展对象申请列表
     */
    //("@ss.hasPermi('system:object:export')")
    @Log(title = "发展对象申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PartyDevelopmentObject partyDevelopmentObject)
    {
        List<PartyDevelopmentObject> list = partyDevelopmentObjectService.selectPartyDevelopmentObjectList(partyDevelopmentObject);
        ExcelUtil<PartyDevelopmentObject> util = new ExcelUtil<PartyDevelopmentObject>(PartyDevelopmentObject.class);
        util.exportExcel(response, list, "发展对象申请数据");
    }

    /**
     * 获取发展对象申请详细信息
     */
    //("@ss.hasPermi('system:object:query')")
    @GetMapping(value = "/{objectId}")
    public AjaxResult getInfo(@PathVariable("objectId") Long objectId)
    {
        return success(partyDevelopmentObjectService.selectPartyDevelopmentObjectByObjectId(objectId));
    }

    /**
     * 新增发展对象申请
     */
    //("@ss.hasPermi('system:object:add')")
    @Log(title = "发展对象申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PartyDevelopmentObject partyDevelopmentObject)
    {
        return toAjax(partyDevelopmentObjectService.insertPartyDevelopmentObject(partyDevelopmentObject));
    }

    /**
     * 修改发展对象申请
     */
    //("@ss.hasPermi('system:object:edit')")
    @Log(title = "发展对象申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PartyDevelopmentObject partyDevelopmentObject)
    {
        return toAjax(partyDevelopmentObjectService.updatePartyDevelopmentObject(partyDevelopmentObject));
    }

    /**
     * 审核发展对象申请
     */
    //("@ss.hasPermi('system:object:edit')")
    @Log(title = "发展对象申请审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody PartyDevelopmentObject partyDevelopmentObject)
    {
        partyDevelopmentObject.setAuditTime(new Date());
        String auditStatus = partyDevelopmentObject.getAuditStatus();

        PartyDevelopmentObject que = partyDevelopmentObjectService.selectPartyDevelopmentObjectByObjectId(partyDevelopmentObject.getObjectId());
        // 如果审核通过，更新用户的当前阶段为发展对象
        if ("1".equals(auditStatus)) {
            sysUserService.updateUserCurrentStage(que.getUserId(), "2");
            partyDevelopmentObject.setApprovalDate(new Date());
        }
        
        return toAjax(partyDevelopmentObjectService.updatePartyDevelopmentObject(partyDevelopmentObject));
    }

    /**
     * 删除发展对象申请
     */
    //("@ss.hasPermi('system:object:remove')")
    @Log(title = "发展对象申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{objectIds}")
    public AjaxResult remove(@PathVariable Long[] objectIds)
    {
        return toAjax(partyDevelopmentObjectService.deletePartyDevelopmentObjectByObjectIds(objectIds));
    }
}
